home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks ’92 / Text Capture FKEY / KeyIsDown.c < prev    next >
Text File  |  1995-09-10  |  1KB  |  33 lines

  1. /******************************************************************************
  2.  KeyIsDown  by Daniel Gimpelevich
  3.  
  4.   Determine whether or not the specified key is being pressed. Keys
  5.   are specified by hardware-specific key code (NOT the character).
  6.  
  7. ******************************************************************************/
  8. Boolean         KeyIsDown( short  theKeyCode );
  9.  
  10. Boolean         KeyIsDown( short  theKeyCode )
  11. {
  12.         KeyMap theKeys;
  13.  
  14.         GetKeys(theKeys);                                     
  15. /* Get state of each key                        */
  16.  
  17.     /* Ordering of bits in a KeyMap is truly bizarre. A KeyMap is a */
  18.     /* 16-byte (128 bits) array where each bit specifies the start  */
  19.     /* of a key (0 = up, 1 = down). We isolate the bit for the      */
  20.     /* specified key code by first determining the byte position in */
  21.     /* the KeyMap and then the bit position within that byte.       */
  22.     /* Key codes 0-7 are in the first byte (offset 0 from the       */
  23.     /* start), codes 8-15 are in the second, etc. The BitTst() trap */
  24.     /* counts bits starting from the high-order bit of the byte.    */
  25.     /* For example, for key code 58 (the option key), we look at    */
  26.     /* the 8th byte (7 offset from the first byte) and the 5th bit  */
  27.     /* within that byte.  */
  28.  
  29.         return( BitTst( ((char*) &theKeys) + theKeyCode / 8,
  30.                         (long) 7 - (theKeyCode % 8) ) );
  31. }
  32.  
  33.